package com.kfm.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.RoomImage;
import com.kfm.hotel.domain.RoomType;
import com.kfm.hotel.domain.SysUser;
import com.kfm.hotel.service.RoomImageService;
import com.kfm.hotel.service.RoomTypeService;
import com.kfm.hotel.util.Constant;
import com.kfm.hotel.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/roomType")
public class RoomTyperController {

    @Autowired
    private RoomTypeService service;

    //前端发送
    @RequestMapping
    public ModelAndView index() {
        ModelAndView andView = new ModelAndView("roomType/index");
        return andView;
    }

    /**
     * 分页请求
     */
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer limit,
                  RoomType roomType) {
        //后端实现分页
        PageInfo<RoomType> list = service.list(roomType, page, limit);
        return R.ok(list);
    }

    /**
     * 添加房间请求
     *
     * @return
     */
    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("roomType/add");
        return modelAndView;
    }

    @PostMapping("/add")
    public R add(@RequestBody RoomType room) {
        //保存房间
//        int i = service.saveRoomType(room);
//        //判断添加是否成功
//        if(i>0){
//            return R.ok("添加成功！",room.getId());
//        }else{
//            return R.fail("添加失败！");
//        }
        boolean save = service.save(room);
        if (save) {
            return R.ok("添加成功！", room.getId());
        } else {
            return R.fail("添加失败！");
        }
    }

        /**
         * 修改房间请求
         */
    @GetMapping("/edit{id}")
    public ModelAndView edit(@PathVariable("id") Integer id){
        ModelAndView modelAndView03 = new ModelAndView("roomType/edit");
        //在后端数据库中查询
        RoomType id1=service.getById(id);
        //查询到的信息放到视图中
        modelAndView03.addObject("room",id1);
        return modelAndView03;
    }
    @PostMapping("/edit")
    public R edit(@RequestBody RoomType room){
        boolean update01 = service.updateById(room);
        if(update01){
            return R.ok("修改成功");
        }else{
            return R.fail("修改失败");
        }
    }

        /**
         * 批量删除房间请求
         * @param ids
         * @return
         */
    @DeleteMapping("/batchDelete")
    public R batchDelete(@RequestBody List<Integer> ids){
        List<RoomType> list = new ArrayList<>();
        //遍历集合
        for (Integer id:ids) {
            //实例化对象
            RoomType roomType01 = new RoomType();
            //确认删除
            roomType01.setIsDel(true);
            roomType01.setId(id);
            list.add(roomType01);
        }
        //更新id
        boolean b = service.updateBatchById(list);
        if(b){
            return R.ok("删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }
        /**
         * 测试批量删除
         * 1.id中必须有值
         * 2.Sysuser中必须添加有参构造方法
         */
    @PostMapping("/test")
    public R test(@RequestBody List<RoomType> roomType01){
        boolean b = service.updateBatchById(roomType01);
        if(b){
            return R.ok("删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }

    }


